package utils;

import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;

import models.cms.Tag;

public class TagUtils {

    public static Set<Tag> text_to_tags(String tagstext) {
        if (StringUtils.isEmpty(tagstext)) {
            return new HashSet<>();
        }
        Set<Tag> tags = new HashSet<>();
        String[] tag_names = tagstext.split(",");
        for (int i = 0; i < tag_names.length; i++) {
            Tag tag = Tag.get_or_create(tag_names[i]);
            tags.add(tag);
        }
        return tags;
    }
    public static String tags_to_text(Set<Tag> tags) {
        return tags.stream().map(t->t.name).collect(Collectors.joining(","));
    }
}
